package gat.api.rest.background.dic;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bitsun.core.common.web.ResultDTO;

import gat.api.background.dic.BDicTypeApi;
import gat.application.dic.DicTypeService;
import gat.dto.req.dic.DicTypeReqDto;
import gat.dto.res.dic.DicTypeResDto;
import gat.dto.res.dic.DicTypeVo;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @Author: tmac
 * @Email: tmac
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/dicType")
public class BDicTypeApiController implements BDicTypeApi {


    @Autowired
    private DicTypeService dicTypeService;

    @Override
    @GetMapping(value = "/getTree", produces = {"application/json"})
    public ResultDTO<DicTypeVo> getTree() {
        List<DicTypeVo> tree = dicTypeService.getTree();
        return ResultDTO.ok(tree);
    }

    @Override
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<DicTypeResDto> addDicType(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody DicTypeReqDto reqDto) {
        DicTypeResDto resDto = dicTypeService.save(reqDto);
        return ResultDTO.ok(resDto);
    }

    @Override
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deleteDicType(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = dicTypeService.deleteByIds(ids);
        return ResultDTO.ok();
    }


    @Override
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<DicTypeResDto> getOneDicType(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        DicTypeResDto resDto = dicTypeService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<DicTypeResDto> patchUpdateDicType(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
        @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody DicTypeReqDto reqDto) {
        dicTypeService.updateProps(id, reqDto);
        DicTypeResDto resDto = dicTypeService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

}
